#include <FS.h>
#include "Arduino.h"

void printFSInfo(){
        FSInfo fs_info;
        SPIFFS.info(fs_info);
        Serial.printf("Total: %d bytes, Used: %d bytes\n", fs_info.totalBytes, fs_info.usedBytes);
}

void writeFS(String path, String data, bool log)
{
    File file = SPIFFS.open(path.c_str(), "w");
    if (file)
    {
        file.println(data.c_str());
        file.close();
        if(log) {
           printFSInfo();
        }
    }
    else
    {
        Serial.println("打开文件失败");
    }
}

void readFS(String path)
{
    File file = SPIFFS.open(path.c_str(), "r");
    if (file)
    {
        while (file.available())
        {
            String line = file.readStringUntil('\n');
            Serial.println(line);
        }
        file.close();
    }
    else
    {
        Serial.println("文件读取失败");
    }
}

void initFS()
{
    if (!SPIFFS.begin())
    {
        Serial.println("SPIFFS 初始化失败！");
        return;
    }

    Serial.println("SPIFFS 初始化成功！");

    printFSInfo();
}